<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { RowType } from './sysLoginLog.data';

import { createVNode } from 'vue';

import { Page, useVbenModal } from '@vben/common-ui';

import { message, Modal } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { querySelectTableApi } from '#/api';
import {
  sysLoginLogDelApi,
  sysLoginLogDeleteBatchApi,
  sysLoginLogQueryPageListApi,
} from '#/api/system/sysLoginLog';
import { useDictionaries } from '#/utils';

import SysLoginLogFormModal from './modules/SysLoginLogFormModal.vue';

const { translateValue } = useDictionaries(['ok_fail']);

const formOptions: VbenFormProps = {
  layout: 'horizontal',
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
  showCollapseButton: false,
  actionButtonsReverse: false,
  showDefaultActions: true,
  submitOnChange: false,
  submitOnEnter: false,
  commonConfig: {
    colon: true,
    labelWidth: 80,
  },
  schema: [
    {
      component: 'ApiSelect',
      label: '用户账号',
      fieldName: 'username',
      componentProps: {
        placeholder: '请选择用户账号',
        allowClear: true,
        showSearch: true,
        api: querySelectTableApi,
        params: {
          tableName: 'sys_user',
          labelField: 'nickname',
          valueField: 'username',
          sqlSelect: 'del_flag = 0',
        },
      },
    },
    {
      component: 'Input',
      label: '登录IP地址',
      fieldName: 'ipaddr',
      componentProps: {
        placeholder: '请输入登录IP地址',
      },
    },
    {
      component: 'Input',
      label: '登录地点',
      fieldName: 'loginLocation',
      componentProps: {
        placeholder: '请输入登录地点',
      },
    },
  ],
};

const gridOptions: VxeTableGridOptions<RowType> = {
  height: 'auto',
  columns: [
    { type: 'checkbox', width: 50, fixed: 'left', align: 'center' },
    { title: '序号', type: 'seq', width: 50, fixed: 'left', align: 'center' },
    {
      title: '用户账号',
      field: 'username',
      align: 'center',
    },
    {
      title: '登录IP地址',
      field: 'ipaddr',
      align: 'center',
    },
    {
      title: '登录地点',
      field: 'loginLocation',
      align: 'center',
    },
    {
      title: '浏览器类型',
      field: 'browser',
      align: 'center',
    },
    {
      title: '操作系统',
      field: 'os',
      align: 'center',
    },
    {
      title: '登录状态',
      field: 'status',
      align: 'center',
      cellRender: {
        name: 'CellDict',
        attrs: {
          dictCode: 'ok_fail',
          translateValue,
        },
      },
    },
    {
      title: '提示消息',
      field: 'msg',
      align: 'center',
    },
    {
      title: '访问时间',
      field: 'loginTime',
      align: 'center',
    },
    {
      title: '主键',
      field: 'id',
      align: 'center',
      visible: false,
    },
    {
      title: '创建人',
      field: 'createBy',
      align: 'center',
      visible: false,
    },
    {
      title: '创建时间',
      field: 'createTime',
      align: 'center',
      visible: false,
    },
    {
      title: '更新人',
      field: 'updateBy',
      align: 'center',
      visible: false,
    },
    {
      title: '更新时间',
      field: 'updateTime',
      align: 'center',
      visible: false,
    },
    // {
    //   title: '操作',
    //   field: 'action',
    //   align: 'center',
    //   width: 210,
    //   fixed: 'right',
    //   slots: {
    //     default: 'action',
    //   },
    // },
  ],
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await sysLoginLogQueryPageListApi({
          pageNo: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
};
const [Grid, gridApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [FromModal, formModalApi] = useVbenModal({
  connectedComponent: SysLoginLogFormModal,
  onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      gridApi.query();
    }
  },
});
async function handlerEdit(row) {
  formModalApi.setData({ id: row.id });
  formModalApi.open();
}
async function handlerDel(row) {
  const msg = await sysLoginLogDelApi(row.id);
  message.success(msg);
  await gridApi.query();
}
async function handlerBatchDel() {
  const checkboxRecords = gridApi.grid.getCheckboxRecords();
  if (checkboxRecords.length === 0) {
    message.warn('至少选中一行');
  } else {
    const idArray = checkboxRecords.map((record) => record.id);
    const ids = idArray.join(',');
    Modal.confirm({
      title: '系统提示?',
      content: createVNode(
        'div',
        { style: 'color:red;' },
        '确认删除所选中行吗?',
      ),
      onOk() {
        sysLoginLogDeleteBatchApi(ids).then(() => {
          message.success('删除成功');
          gridApi.query();
        });
      },
      class: 'test',
    });
  }
}
</script>

<template>
  <Page auto-content-height>
    <FromModal class="w-[50%]" />
    <Grid>
      <!--      <template #toolbar-buttons>-->
      <!--        <a-button type="primary" class="mr-2" @click="handlerAdd()">-->
      <!--          新增-->
      <!--        </a-button>-->
      <!--      </template>-->
      <template #toolbar-tools>
        <a-button type="primary" danger @click="handlerBatchDel()">
          批量删除
        </a-button>
      </template>
      <template #action="{ row }">
        <a-button type="link" size="small" @click="handlerEdit(row)">
          编辑
        </a-button>
        <a-popconfirm
          cancel-text="再想想"
          title="您确定要删除吗?"
          @confirm="handlerDel(row)"
        >
          <a-button type="link" danger size="small"> 删除 </a-button>
        </a-popconfirm>
      </template>
    </Grid>
  </Page>
</template>
